<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Trung Tin
 * Date: 8/31/12
 * Time: 3:01 PM
 * Description :
 */
function getdata($url,$data){
    $curl=curl_init();

    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl,CURLOPT_POST,1);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
    curl_setopt($curl, CURLOPT_TIMEOUT, 60);

    $html=curl_exec($curl);
    curl_close($curl);
    return $html;
}
function getdatas($url,$datas){
    $url=$url;
    $mh=curl_multi_init();
    $curl=array();
    $i=0;
    foreach($datas as $key){
        $postdata="action=download&songkey=$key&saveas=Save+Link+As...+";
        $curl[$i]=curl_init();

        curl_setopt($curl[$i],CURLOPT_URL,$url);
        curl_setopt($curl[$i],CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl[$i],CURLOPT_POST,1);
        curl_setopt($curl[$i],CURLOPT_POSTFIELDS,$postdata);
        curl_setopt($curl[$i], CURLOPT_TIMEOUT, 60);

        curl_multi_add_handle($mh,$curl[$i]);
        $i++;
    }
    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while ($running);

    $html="";
    for($j=0;$j<$i;$j++)
    {
        $html.=curl_multi_getcontent($curl[$j]);
    }
    return $html;
}
function getnhacso($url){
	$html=file_get_contents($url);
	//echo $html;
	
	$rule='/<link href=\".*\?xmlPath=(.*)&amp;adsLink=/isU';

	if(preg_match($rule,$html,$xml)){	
		$xml=$xml[1];
		$sxml=simplexml_load_file($xml);		
		if($sxml){
			$listsong=$sxml->playlist->song;
			$rs="";
			foreach($listsong as $song){
				$rs.=$song->mp3link."\n";			
			}
			return $rs;			
		}
	}
}

 if($_POST["submit"])
 {
	
 	if($_POST["link"]!="")
	{
		
		$url=$_POST["link"];
		
		if(strpos($url,"nhacso.net")){
			$linknhac=getnhacso($url);			
			
		}elseif(strpos($url,"nhaccuatui.com"))
		{
			$html=file_get_contents($_POST["link"]);
			$rule='/<a id=\"playlist-song\" key=\"(.*)\"/isU';

			if(preg_match($rule,$html,$key))
			{
				$key=$key[1];
				$url="http://www.nhaccuatui.com/api/playlist.ashx";
				$postdata="action=get_song_of_playlist&listkey=".$key."&ismore=false";
				$html=getdata($url,$postdata);
				$rule='/key=\\\"(.*)\\\">/isU';
				if(preg_match_all($rule,$html,$keys)){
					$keys=$keys[1];
					$url="http://www.nhaccuatui.com/api/song.ashx";
					$html=getdatas($url,$keys);

					$html=str_replace("&#58;",":",$html);
					$html=str_replace("&#47;","/",$html);
					$rule='/href=\\\"(.*)\\\"/isU';
					if(preg_match_all($rule,$html,$links))
					{

						$linknhac="";
						foreach ($links[1] as $link) {
							$linknhac.=$link."\n";
						}

					}

				}

			}
		}


	}
 }
 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>GET Nhac Playlist</title>
<style>
form{
	width:700px;
	#margin:auto;
}
label{
	display:inline-block;
	width:100px;
	vertical-align:top;	
}
</style>
</head>

<body>

<form action="#" method="post">
<fieldset>
<legend>Dien Link Playlist hoac Album de lay link direct tu trang nhaccuatui.com hoac nhacso.net</legend>
<label>Link Playlist</label><input type="text" name="link" size="25" value="<?php if(isset($_POST["link"])) echo $_POST["link"]; ?>" /><input type="submit" name="submit" value=" Get Link " /><br />
<label>Link Direct</label><textarea cols="45" rows="10" onclick="javascript:this.select()">
<?php if(isset($linknhac)) echo $linknhac; ?>
</textarea>



</fieldset>
</form>

</body>
</html>